<?php
/**
 * @author 	 	 supfire@sohu.com
 * @copyright		 ourmy.com
 * @version		 1.0
 * @name 		 user.php
 * @todo 		 用户action
 * @final 		 2009-05-21
 */
define('IN_YCTOOL', true);//定义该文件合法性
require_once('config.php');
require_once(DIR_COMM . 'common.php');
require_once(DIR_CTRL . 'msg.func.php');
$menu = 5;//导航为普通用户。
$user->isLogin('', $_GET['uid']);//没有登录 就跳转到登陆页面.
$userid = $user->userinfo['userid'];
$email = $user->userinfo['email'];
$username = $user->userinfo['username'];
$enname = $user->userinfo['enname'];
$roles = $user->userinfo['roles'];
$mobile = $user->userinfo['mobile'];
$action	= isset($_GET['action']) ? $_GET['action'] : "";
$actionPost = isset($_POST['actionPost']) ? $_POST['actionPost'] : "";

//读取user的目录路径
$userid = $user->userinfo['userid'];
$useridpath = 'userdata/'.func::getFilePath($userid);
//读取朋友列表
if(file_exists($useridpath.'myfriend.php'))
{
    require_once($useridpath.'myfriend.php');
}

foreach ($arr_all_friend as $key => $item)
{
    $friendid .= $item['friendid'] . ',';
}
$friendid = substr($friendid, 0, -1);
$friendarray = db::getALl(DB_TABLEPRE.'user_basic', 'userid,username', "userid IN ($friendid) AND active <> 0 AND roles <> 7");
if($friendarray)
{
    foreach ($friendarray as $key => $item)
    {
        $friendidt = $item['userid'];
        $arr_friend[$friendidt][friendid] = $item['userid'];
        $arr_friend[$friendidt][username] = $item['username'];
    }  
}
$arr_all_friend = $arr_friend;
if(!$arr_all_friend)
{
    $arr_all_friend = array();
}


$page = intval($_GET["page"]);
if ($page < 1)
{
	$page = 1;
}
//操作
readNewNoteMsg($userid);
if($_GET['del'] == 1)
{
    $msgid = isset($_GET['msgid']) ? $_GET['msgid'] : 0;	
    $sysid = isset($_GET['sysid']) ? $_GET['sysid'] : 0;	
    $all = isset($_GET['all']) ? $_GET['all'] : 0;
    
    if($_GET['act'] == 'to')
    {
	msg::deleteToMe($userid, $msgid);
	$test = '删除成功';
    }
    elseif($_GET['act'] == 'from')
    {
	msg::deleteFromMe($userid, $msgid);
	$test = '删除成功';
    }
    elseif($_GET['act'] == 'ft')
    {
	msg::deleteFT($userid, $msgid, $all);
	$test = '删除成功';
    }
    elseif($_GET['act'] == 'fromall')
    {
	//msg::deleteFromMeAll($userid, $msgid);
	//$test = '发件箱已经清空';
    }
    elseif($_GET['act'] == 'toall')
    {
	//msg::deleteToMeAll($userid, $msgid);
	//$test = '收件箱已经清空';
    }
    elseif($_GET['act'] == 'one')
    {
	msg::deleteSysOne($userid, $sysid);
	$test = '删除成功';
    }
    elseif($_GET['act'] == 'all')
    {
	$action == 'sys' && $t = 'type <> 2 AND type <> 3 AND type <> 4 AND type <> 12 AND type <> 13';
	$action == 'reply' && $t = 'type = 3';
	$action == 'replycomment' && $t = 'type = 4';
	$action == 'act' && $t = $t = 'type = 12';
	$action == 'gift' && $t = $t = 'type = 11';
	msg::deleteSysMsg($userid, $t);
	unset($t);
	$test = '已经清空';
    }
}
switch ($action)
{
case 'sound':
    exit;
    break;

case 'write':
    $uidmsg	= isset($_GET['uid']) ? $_GET['uid'] : 0;
    unset($_GET['uid']);
    if($actionPost == 'add')
    {//touid
	$Recipient	    = isset($_POST['Recipient']) ? $_POST['Recipient'] : '';
	$title	    = isset($_POST['title']) ? $_POST['title'] : '';
	$message    = isset($_POST['message']) ? $_POST['message'] : '';
        $message    = str_replace(" ", "&nbsp;", $message);
        $message    = str_replace("\n", "<br />", $message);
        $message    = str_replace("\\", "", $message);

	if(!$message || !$title || !$Recipient)
	{
	    func::alertMsg('收件人或者主题或者发送内容为空');
	}
	if($Recipient)
    {
	    $Recipientrs	    = explode(",", $Recipient);
	    $friendid = '';
	    if(array_unique($arr_all_friend))
	    {
		$countfriend = count($arr_all_friend);
		foreach ($arr_all_friend as $key => $item)
		{
		    $friendid .= $item['friendid'] . ',';
		}
		$friendid = substr($friendid, 0, -1);
	    }

	    $friendarray = db::getALl(DB_TABLEPRE.'user_basic', 'userid,username', "userid IN ($friendid)");

	    if($friendarray)
	    {
		$countfriend = count($friendarray);
		foreach ($friendarray as $key => $item)
		{
		    if( in_array($item['username'], $Recipientrs))
		    {
			$touid .= $item['userid'] . ',';
		    }
		}
		//$touid = substr($touid, 0, -1);
	    }
	    (!$touid && $uidmsg ) && $touid .= $uidmsg . ',';

	    if(strlen($touid) > 254)
	    {
		 func::alertMsg('选择好友过多，请返回减少好友发送！');	
	    }

	    if(strlen($touid) < 1)
	    {
		 func::alertMsg('好友不存在或者没填写！');	
	    }

	    if(strpos($touid, $userid . ',') !== false)
	    {
		 func::alertMsg('自己不能给自己发邮件！');	
	    }

	    if(msg::sendMessage($touid, $userid, $title, $message))
	    {
		func::obHeader('msg.php?action=fromme&to=1');
	    }
	}
	else
	{
	    func::alertMsg('好友不存在或者没填写');	
	}
    }
     $uname = db::getOne(DB_TABLEPRE.'user_basic', 'username', "userid IN ($uidmsg)");
    break;

case 'tome':
 if($_GET['act'] == 'isreply')
    {
	$isreply	= isset($_GET['isreply']) ? $_GET['isreply'] : 0;
	$sysid		= isset($_GET['sysid']) ? $_GET['sysid'] : 0;
	$msgid = isset($_GET['msgid']) ? $_GET['msgid'] : 0;
	$friendid = isset($_GET['friendid']) ? $_GET['friendid'] : 0;

	@include_once($useridpath.'tomyfriend.php');

	if($isreply == 1 && $arr_all_tomyfriend[$friendid]['username'])
	{
	    //添加为自己的好友
	    $ret = $user->getUserInfoById($friendid);
	    $addtime = date('Y-m-d H:i:s');
	    $grouptype = "";
        
	    $username = $ret['username'];
	    $enname = $ret['enname'];

            //发邮件begin ccemail为0及时发送，sendok为0没有发送的
            $ccemail = $useremail = db::getOne(DB_TABLEPRE . 'msg_thread', 'ccemail', 'sendok = 0 AND msgid = '.$msgid);
            if($ccemail == 0)
            {
                require_once(DIR_COMM . 'cuthtml.func.php');
                require_once(DIR_COMM . 'sendmail.php');  
		$userrs = db::getRow(DB_TABLEPRE . 'user_basic', 'username,email', 'userid = '.$friendid);
		$useremail = $userrs[email];
		$userrsusername = $user->userinfo['username'];
                msg::Msendemail($friendid, '', $useremail, "您和 $userrsusername 成为好友", $msgid);
            }
            //发邮件end

	    $arr = array();
	    $arr_sub = array();
	    $arr_sub["friendid"] = $friendid;
	    $arr_sub["time"] = $addtime;
	    $arr_sub["grouptype"] = $grouptype;
	    $arr_sub["username"] = $username;
	    $arr_sub["enname"] = $enname;
	    $arr[$friendid] = $arr_sub;
	    friend::operateFriendFile($userid, $arr, 'myfriend.php', 'php', 2, 'userdata/');

	    //将自己加为对方的好友好友
	    $arr2 = array();
	    $arr_sub2 = array();
	    $arr_sub2["friendid"] = $userid;
	    $arr_sub2["time"] = date('Y-m-d H:i:s');
	    $arr_sub2["grouptype"] = $grouptype2;
	    $arr_sub2["username"] = $user->userinfo['username'];
	    $arr_sub2["enname"] = $user->userinfo['enname'];
	    $arr2[$userid] = $arr_sub2;

	    friend::operateFriendFile($friendid, $arr2, 'myfriend.php', 'php', 1, 'userdata/');

	    //从申请列表中删除
	    unset($arr_all_tomyfriend[$friendid]);
	    func::operateFile('arr_all_tomyfriend', $arr_all_tomyfriend, 'tomyfriend.php', '', 2, $useridpath);
	    //发消息
	    db::updateRs(DB_TABLEPRE . 'msg_thread', 'isreply = 1', 'type = 1 AND fromuid = '.$friendid.' AND touid = \''.$userid.',\'');


	}
	if ($isreply == 2 && $arr_all_tomyfriend[$friendid]['username'])//忽略
	{
	    //从申请列表中删除
            unset($arr_all_tomyfriend[$friendid]);
            $ret = func::operateFile("arr_all_tomyfriend", $arr_all_tomyfriend, 'tomyfriend.php', '', 2, $useridpath);
             //发消息
	    db::updateRs(DB_TABLEPRE . 'msg_thread', 'isreply = 2', 'type = 1 AND fromuid = '.$friendid.' AND touid = \''.$userid.',\'');
	}

	func::obHeader("msg.php?action=tome");

    }

    readNewNote($userid, 1);
    $per = 8;
    $url = "msg.php?action=tome";
    $count = msg::toMeMCount($userid);
    $rs = msg::toMeMessage($userid, $page, $per);
    break;

case 'fromme':
    $per = 8;
    $url = "msg.php?action=fromme";
    $count = msg::fromMeMCount($userid);
    $rs = msg::fromMeMessage($userid, $page, $per);

    foreach($rs as $v)
    {
	$touidstr .= $v['touid'];
	$touidstr .= $v['fromuid'].',';
    }

    $touidstr = explode(',', $touidstr);
    $touidstr = array_unique($touidstr);

    foreach($touidstr as $v)
    {
	$v && $touids[] = $v;
    }
    $touidstr = implode(',', $touids);

    $arr_all_friend_rs = db::getLimit( DB_TABLEPRE . 'user_basic', 
	    'userid as friendid, username',
	    "userid IN ($touidstr) ");

    foreach($arr_all_friend_rs as $v)
    {
	$ket = $v['friendid'];
	$arr_all_friend_arr[$ket] = $v;
    }

    break;

case 'comment':
    $msgid		= isset($_GET['msgid']) ? $_GET['msgid'] : 0;
    $mid		= isset($_GET['mid']) ? $_GET['mid'] : 0;

    if(!$msgid)
    {
	func::obHeader('msg.php?action=write');
    }

    if($actionPost == 'add')
    {
        $touid	    = isset($_POST['touid']) ? $_POST['touid'] : 0;
        $title	    = isset($_POST['title']) ? $_POST['title'] : '';

	$message    = isset($_POST['message']) ? $_POST['message'] : '';
	if(!$message)
	{
	    $textmsg = '请您在下面填写发送内容';
	}
	$message = htmlspecialchars($message);
	$message = str_replace(' ', '&nbsp;', $message);
	$message = str_replace("\n", '<br />', $message);

	if($touid)
        {
            $mid = msg::sendComment($msgid, $touid.',', $userid, $title, $message);
	    if($mid)
	    {
		func::obHeader('msg.php?action=comment&fromto='.$_GET['fromto'].'&mid='.$mid.'&msgid='.$msgid.'#post');
	    }
	}
    }

    $title = db::getOne(DB_TABLEPRE.'msg_thread', 'title', "msgid = $msgid");

    $per = 8;
    $url = "msg.php?action=comment&fromto=".$_GET['fromto']."&mid=$mid&msgid=$msgid";
    $count = msg::oneMsgCount($msgid, $mid);
    $rs = msg::oneMsgList($msgid, $page, $per, $userid, $mid);
    break;

case 'reply':
    readNewNote($userid, 3);
    $per = 8;
    $url = "msg.php?action=reply";
    $count = msg::msgSysCount($userid, 'type = 3');
    $rs = msg::msgSys($userid, 'type = 3', $page, $per);
    break;

case 'replycomment'://评论回复
    readNewNote($userid, 4);
    $per = 8;
    $url = "msg.php?action=replycomment";
    $count = msg::msgSysCount($userid, 'type = 4');
    $rs = msg::msgSys($userid, 'type = 4', $page, $per);
    break;

case 'act'://动了一下
    if($_GET['act'] == 'isreply')
    {
	$isreply	= isset($_GET['isreply']) ? $_GET['isreply'] : 0;
	$actid		= isset($_GET['actid']) ? $_GET['actid'] : 0;
	$sysid		= isset($_GET['sysid']) ? $_GET['sysid'] : 0;
	$friendid	= isset($_GET['friendid']) ? $_GET['friendid'] : 0;

	if($isreply == 3)
	{
	    msg::insertMsgSys($friendid, $userid, 12, 12, $actid);
	    echo '<SCRIPT language=JavaScript>alert("您的动作已经发送出去，好友登录时，会看到这个动作。");history.back();</SCRIPT>';
	    exit;
	}
	elseif($isreply == 1)
	{
	    msg::insertMsgSys($friendid, $userid, 12, 12, $actid);
	    msg::isSysMsg($userid, $sysid, 1);
	    func::obHeader('msg.php?action=act');
	}
	elseif($isreply == 2)
	{
	    msg::isSysMsg($userid, $sysid, 2);
	    func::obHeader('plugins/act/index.php?uid='.$friendid);
	}
        $text = '您的动作已经发送出去，好友登录时，会看到这个动作。';
    }
    readNewNote($userid, 12);
    $per = 8;
    $url = 'msg.php?action=act';
    $count = msg::msgSysCount($userid, 'type = 12');
    $rs = msg::msgSys($userid, 'type = 12', $page, $per);
    break;

case 'gift'://礼物
    if($_GET['act'] == 'isreply')
    {
	$isreply	= isset($_GET['isreply']) ? $_GET['isreply'] : 0;
	$sysid		= isset($_GET['sysid']) ? $_GET['sysid'] : 0;
	$friendid	= isset($_GET['friendid']) ? $_GET['friendid'] : 0;

	if($isreply == 2)
	{
	    msg::isSysMsg($userid, $sysid, 2);
	    func::obHeader('plugins/gift/index.php?uid='.$friendid);
	}
    }
    
    $uid	= isset($_GET['uid']) ? $_GET['uid'] : 0;
    $per = 8;
    $count = msg::msgSysCount($uid, 'type = 11');
    if($uid)//访问者访问
    {
	$useridpath = 'userdata/'.func::getFilePath($uid);
	@include_once($useridpath.'myfriend.php');
	$rs = msg::msgSys($uid, 'type = 11', $page, $per);	

	$url = 'msg.php?action=gift&uid='.$uid;
    }
    else//主人访问
    {
	readNewNote($userid, 11);
        $rs = msg::msgSys($userid, 'type = 11', $page, $per);
	$url = 'msg.php?action=gift';
    }
    break;

default:;
    //消息首页
    if (file_exists($useridpath.'config/newnote.php'))
    {
	require_once($useridpath.'config/newnote.php');
    }
    else
    {
	$variablenewnote = array();
	//$variablenewnote[$userid]['newmsg'] = 0;
	//$variablenewnote[$userid]['sysmsg'] = 0;
	//$variablenewnote[$userid]['commentmsg'] = 0;
	//$variablenewnote[$userid]['recommentmsg'] = 0;
    }

}
require_once DIR_COMM.'template.func.php';
require_once tpl('msg'.$action.'.html', $tpldir);
exit;
?>
